package com.hllxd.hism.star.basic.service.service;

import com.hllxd.hism.star.basic.service.collect.CollectYarn;
import com.hllxd.hism.star.basic.service.dao.redis.YarnRedisMapper;
import com.hllxd.hism.star.basic.service.util.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author lixiyong
 * @date 20210821
 */
@Slf4j
@Service("realtimeService")
public class RealtimeServiceImpl implements RealtimeService {

    @Autowired
    private YarnRedisMapper redisMapper;

    @Autowired
    private CollectYarn collectYarn;

    
    final static Long GAP = 60L;
    @Override
    public Boolean requestYarn() {
        Long lastTsApi = redisMapper.getRequestLastTs("api");
        Long lastTsYarn = redisMapper.getRequestLastTs("yarn");
        Long currTs = TimeUtil.currentTimeHllDT();
        try {
            if (lastTsApi > lastTsYarn + GAP) {
                redisMapper.setRequestLastTs("yarn",currTs);
                log.info("lastTsApi: {}, lastTs: {}", lastTsApi, lastTsYarn);
                collectYarn.listYarnAppRequest();
                collectYarn.schedulerRequest();
            }
            return true;
        }catch (Exception e) {
            log.error("Exception: {}, {}.", lastTsYarn, e.toString());
            redisMapper.setRequestLastTs("yarn", lastTsYarn);
            return false;
        }
    }
}
